package com.world.blog.service.impl;

import java.util.List;

import com.world.common.utils.AssertUtil;
import com.world.common.utils.DateUtils;
import com.world.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.world.blog.mapper.CategoryMapper;
import com.world.blog.domain.BlogCategory;
import com.world.blog.service.IBlogCategoryService;

/**
 * 分类Service业务层处理
 * 
 * @author LiJunXi
 * @date 2025-05-19
 */
@Service
public class BlogCategoryServiceImpl implements IBlogCategoryService
{
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询分类
     * 
     * @param id 分类主键
     * @return 分类
     */
    @Override
    public BlogCategory selectCategoryById(Long id)
    {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询分类列表
     * 
     * @param blogCategory 分类
     * @return 分类
     */
    @Override
    public List<BlogCategory> selectCategoryList(BlogCategory blogCategory)
    {
        if (!SecurityUtils.isAnonymous() && !SecurityUtils.isBlogAdmin() && !SecurityUtils.isAdmin()) {
            blogCategory.setUserId(SecurityUtils.getUserId());
        }
        return categoryMapper.selectCategoryList(blogCategory);
    }

    /**
     * 新增分类
     * 
     * @param blogCategory 分类
     * @return 结果
     */
    @Override
    public int insertCategory(BlogCategory blogCategory)
    {
        blogCategory.checkParams();
        blogCategory.setUserId(SecurityUtils.getUserId());
        blogCategory.setCreateTime(DateUtils.getNowDate());
        return categoryMapper.insertCategory(blogCategory);
    }

    /**
     * 修改分类
     * 
     * @param blogCategory 分类
     * @return 结果
     */
    @Override
    public int updateCategory(BlogCategory blogCategory)
    {
        blogCategory.checkParams();
        BlogCategory find = categoryMapper.selectCategoryById(blogCategory.getId());
        AssertUtil.isNotEmpty(find, "分类不存在");
        AssertUtil.isTrue(SecurityUtils.getUserId().equals(find.getUserId()), "权限不足");
        find.setName(blogCategory.getName());
        find.setUpdateTime(DateUtils.getNowDate());
        return categoryMapper.updateCategory(find);
    }

    /**
     * 批量删除分类
     * 
     * @param ids 需要删除的分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除分类信息
     * 
     * @param id 分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        return categoryMapper.deleteCategoryById(id);
    }
}
